/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Tela;

import Conexao.ConectaBD;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.proteanit.sql.DbUtils;

/**
 *
 * @author Thalyson
 */
public class Tela_CadastrarSolicitante extends javax.swing.JFrame {

    Connection conecta;
    PreparedStatement pst;
    ResultSet rs;

    public Tela_CadastrarSolicitante() throws ClassNotFoundException {
        initComponents();
        setSize(806, 500);
        conecta = ConectaBD.conectabd();
        listarSolicitantes();
        setLocationRelativeTo(null);
    }

    public void cadastrarSolicitante() {
        String sql = "Insert into solicitante (NOME,TIPO,NUM_IDENTIF,SETOR,ENDERECO,TELEFONE) values(?,?,?,?,?,?)";

        if (tf_nome.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Campo NOME é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (cb_tipo.getSelectedItem().equals("TIPO")) {
            JOptionPane.showMessageDialog(null, "Campo TIPO é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (cb_setor.getSelectedItem().equals("SETOR")) {
            JOptionPane.showMessageDialog(null, "Campo SETOR é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (tf_endereco.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Campo ENDEREÇO é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (jf_telefone.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "Campo TELEFONE é Obrigatório", "Aviso", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            pst = conecta.prepareStatement(sql);
            pst.setString(1, tf_nome.getText());
            pst.setString(2, (String) cb_tipo.getSelectedItem());
            pst.setString(3, jf_numero.getText());
            pst.setString(4, (String) cb_setor.getSelectedItem());
            pst.setString(5, tf_endereco.getText());
            pst.setString(6, jf_telefone.getText());

            pst.execute();
            JOptionPane.showMessageDialog(null, "Cadastrado com sucesso!", "Cadastrado com sucesso", JOptionPane.INFORMATION_MESSAGE);
            listarSolicitantes();
            Limpar();
        } catch (SQLException error) {
            JOptionPane.showMessageDialog(null, error);
            listarSolicitantes();
        }
    }

    public void listarSolicitantes() {
        String sql = "Select *from solicitante order by NOME Asc";
        try {
            pst = conecta.prepareStatement(sql);
            rs = pst.executeQuery();
            jt_solicitantes.setModel(DbUtils.resultSetToTableModel(rs));
        } catch (SQLException error) {
            JOptionPane.showMessageDialog(null, error);
        }
    }

    public void mostraSolicitantes() {
        int seleciona = jt_solicitantes.getSelectedRow();
        tf_id.setText(jt_solicitantes.getModel().getValueAt(seleciona, 0).toString());
        tf_nome.setText(jt_solicitantes.getModel().getValueAt(seleciona, 1).toString());
        cb_tipo.setSelectedItem(jt_solicitantes.getModel().getValueAt(seleciona, 2).toString());
        jf_numero.setText(jt_solicitantes.getModel().getValueAt(seleciona, 3).toString());
        cb_setor.setSelectedItem(jt_solicitantes.getModel().getValueAt(seleciona, 4).toString());
        tf_endereco.setText(jt_solicitantes.getModel().getValueAt(seleciona, 5).toString());
        jf_telefone.setText(jt_solicitantes.getModel().getValueAt(seleciona, 6).toString());
    }

    public void Limpar() {
        tf_nome.setText("");
        jf_numero.setText("");
        tf_id.setText("");
        cb_tipo.setSelectedItem("TIPO");
        cb_setor.setSelectedItem("SETOR");
        tf_endereco.setText("");
        jf_telefone.setText("");
    }

    public void editarSolicitantes() {
        String sql = "Update solicitante set NOME = ?,TIPO = ?,NUM_IDENTIF = ?,SETOR = ?,ENDERECO = ? ,TELEFONE = ? where id = ? ";

        try {
            pst = conecta.prepareStatement(sql);
            pst.setString(1, tf_nome.getText());
            pst.setString(2, cb_tipo.getSelectedItem().toString());
            pst.setString(3, jf_numero.getText());
            pst.setString(4, cb_setor.getSelectedItem().toString());
            pst.setString(5, tf_endereco.getText());
            pst.setString(6, jf_telefone.getText());
            pst.setInt(7, Integer.parseInt(tf_id.getText()));
            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Solicitante atualizado com sucesso");
            listarSolicitantes();

        } catch (SQLException error) {
            JOptionPane.showMessageDialog(null, error);
        }
    }

    public void deletarSolicitante() {
        String sql = "Delete from solicitante where id = ?";

        try {
            pst = conecta.prepareStatement(sql);
            pst.setInt(1, Integer.parseInt(tf_id.getText()));
            pst.execute();
            JOptionPane.showMessageDialog(null, "Solicitante deletado com sucesso");
            listarSolicitantes();
        } catch (SQLException error) {

            JOptionPane.showMessageDialog(null, error);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt_solicitantes = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        cb_tipo = new javax.swing.JComboBox();
        jButton5 = new javax.swing.JButton();
        cb_setor = new javax.swing.JComboBox();
        tf_endereco = new javax.swing.JTextField();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        tf_id = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        tf_nome = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jf_telefone = new javax.swing.JFormattedTextField();
        jLabel8 = new javax.swing.JLabel();
        jf_numero = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("E-KEY - Cadastrar Funcionário/Estagiário");
        setPreferredSize(new java.awt.Dimension(800, 440));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("TIPO DO SOLICITANTE");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(10, 70, 175, 15);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel6.setText("ENDEREÇO");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(10, 130, 64, 15);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("ID");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(750, 10, 40, 15);

        jt_solicitantes.setAutoCreateRowSorter(true);
        jt_solicitantes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jt_solicitantes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jt_solicitantesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jt_solicitantes);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(0, 190, 800, 210);

        jButton3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Working (1).png"))); // NOI18N
        jButton3.setText("LIMPAR");
        jButton3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(470, 410, 120, 50);

        cb_tipo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cb_tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "TIPO", "FUNCIONÁRIO", "ESTAGIÁRIO" }));
        cb_tipo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cb_tipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_tipoActionPerformed(evt);
            }
        });
        getContentPane().add(cb_tipo);
        cb_tipo.setBounds(10, 90, 175, 30);

        jButton5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Eraser (1).png"))); // NOI18N
        jButton5.setText("EXCLUIR");
        jButton5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);
        jButton5.setBounds(340, 410, 120, 50);

        cb_setor.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cb_setor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "SETOR", "LIMPEZA", "SEGURANÇA", "OBRA", "TECNOLOGIA" }));
        cb_setor.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(cb_setor);
        cb_setor.setBounds(390, 30, 269, 30);

        tf_endereco.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_endereco.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(tf_endereco);
        tf_endereco.setBounds(10, 150, 424, 30);

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Finish  (1).png"))); // NOI18N
        jButton4.setText("SALVAR");
        jButton4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);
        jButton4.setBounds(80, 410, 120, 50);

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Information (1).png"))); // NOI18N
        jButton1.setText("EDITAR");
        jButton1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(210, 410, 120, 50);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("NÚMERO DE IDENTIFICAÇÃO");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(200, 70, 175, 15);

        tf_id.setEditable(false);
        tf_id.setBackground(new java.awt.Color(255, 255, 255));
        tf_id.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_id.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        getContentPane().add(tf_id);
        tf_id.setBounds(750, 30, 40, 30);

        jButton2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Cancel (1).png"))); // NOI18N
        jButton2.setText("FECHAR");
        jButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(600, 410, 120, 50);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("SETOR");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(390, 10, 269, 15);

        tf_nome.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        tf_nome.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        tf_nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_nomeActionPerformed(evt);
            }
        });
        getContentPane().add(tf_nome);
        tf_nome.setBounds(10, 30, 368, 30);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("TELEFONE");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(450, 130, 58, 15);

        jf_telefone.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        try {
            jf_telefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##)####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jf_telefone.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        getContentPane().add(jf_telefone);
        jf_telefone.setBounds(450, 150, 142, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("NOME");
        getContentPane().add(jLabel8);
        jLabel8.setBounds(10, 10, 368, 15);

        jf_numero.setEditable(false);
        jf_numero.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        try {
            jf_numero.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("    ##-#-#####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jf_numero.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        getContentPane().add(jf_numero);
        jf_numero.setBounds(200, 90, 140, 30);

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icones/Fundo E-Key.png"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 800, 500);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jt_solicitantesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jt_solicitantesMouseClicked
        mostraSolicitantes();
    }//GEN-LAST:event_jt_solicitantesMouseClicked

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        Limpar();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        deletarSolicitante();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        cadastrarSolicitante();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        editarSolicitantes();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tf_nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_nomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_nomeActionPerformed

    private void cb_tipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_tipoActionPerformed
        try {
            if (cb_tipo.getSelectedItem() == "ESTAGIÁRIO") {
                jf_numero.setEditable(true);
            } else {
                jf_numero.setEditable(false);
            }
        } catch (Exception e) {
        }
    }//GEN-LAST:event_cb_tipoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Tela_CadastrarSolicitante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Tela_CadastrarSolicitante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Tela_CadastrarSolicitante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Tela_CadastrarSolicitante.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new Tela_CadastrarSolicitante().setVisible(true);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Tela_CadastrarSolicitante.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_setor;
    private javax.swing.JComboBox cb_tipo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JFormattedTextField jf_numero;
    private javax.swing.JFormattedTextField jf_telefone;
    private javax.swing.JTable jt_solicitantes;
    private javax.swing.JTextField tf_endereco;
    private javax.swing.JTextField tf_id;
    private javax.swing.JTextField tf_nome;
    // End of variables declaration//GEN-END:variables
}
